var myParameter = myApp.directive('myParameter', ['dataSource', function (dataSource) {
    return {
        restrict: 'E',
        scope: {
            csModel: '=ngModel',
            csClassCode: '@csClass',
            csReadonly: '=ngReadonly',
            multiple: '=multiple',
            ngRequired: '=ngRequired',
            csContainAllItems: '@csContainAllItems',
            ngDisabled:'='
        },
        link: function (scope, elem, attrs) {
            scope.valueField = attrs.csValueField;

            // scope.csModel = ngModel;
            if (!scope.valueField) scope.valueField = "IntId";

            scope.csContainAllItems = false;
            if (attrs.csContainAllItems) scope.csContainAllItems = true;

            if (attrs.name) {
                $(elem).find('#csperiod_select_input').attr('name', attrs.name);
                if (attrs.required) {
                    $(elem).find('input').attr('required', true);
                }
                if (attrs.csValidate) {
                    $(elem).find('input').attr('cs-validate', attrs.csValidate);
                }
                if (attrs.csValidates) {
                    $(elem).find('input').attr('cs-validates', attrs.csValidates);
                }
                if (attrs.ngDisabled) {
                    $(elem).find('select').attr('ng-disabled', attrs.ngDisabled);
                }
            }
        },
        controller: ['$scope', '$http', function ($scope, $http) {
            //$scope.parameterClass = {};
            //$scope.data = [];
            $scope.text = "";
            var SetText = function (data) {
                var items = data.Data;
                if ($scope.csContainAllItems) {
                    items = angular.copy(data.Data);
                    items.splice(0, 0, { IntId: 0, Text: "全部", StringId: "" });
                }

                if ($scope.csModel) {
                    for (var i = 0; i < items.length; i++) {
                        if (items[i][$scope.valueField] === $scope.csModel) {
                            $scope.text = items[i].Text;
                            break;
                        }
                    }
                }
                $scope.data = items;
            };
            $scope.$watch("csModel", function () {
                init();
            })
            var init = function () {
                dataSource.getParameter($scope.csClassCode).then(function (data) {
                    if (data) {
                        SetText(data);
                    }
                });
                
            };

            // init();


        }],
        template: function (elem, attrs) {
            var valueField = attrs.csValueField;
            if (!valueField) valueField = "IntId";
            var mode = attrs.csMode;
            if (!mode) mode = "edit";

            if (mode == "view") {
                return '<label>{{text}}</label>'
            }
            else if (attrs.ngReadonly) {
                return '<label class="form-control-static" name="' + attrs.name + '">{{text}}</label>'
            }
            else {
                return '<select ng-disabled="ngDisabled" class="form-control" ng-model="csModel" name="' + attrs.name + '" ng-required="{{ngRequired}}" ng-options="option.' + valueField + ' as option.Text for option in data" enable-validate="true" ></select>';
            }
        }
    };
}]);